<template>
  <div class="card" style="margin-bottom: 10px">
    <el-table :data="tableData" style="width: 100%" stripe :header-cell-style="{backgroundColor: 'aliceblue', color: '#666'}" @selection-change="handleSelectionChange">

      <el-table-column prop="orderNum" label="订单号"/>
      <el-table-column prop="goodsName" label="订单名"/>
      <el-table-column prop="userId" label="下单用户"/>
      <el-table-column label="操作" width="180">
        <template v-slot="scope">
          <el-button type="danger" @click="writeOff(scope.row)">核销</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <div style="margin: 10px 0">
    <div class="example-pagination-block">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import {ElMessage} from "element-plus";

export default {
  name: "WriteOff",
  data() {
    return {
      phone: '',
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      name: '',
      orderNum: '',
      order: {},
      user : JSON.parse(localStorage.getItem('honey-user')),

    };
  },
  created() {
    this.phone = this.$route.params.phone;
    console.log(this.phone);
    this.load();
  },
  methods: {
    load(pageNum) { // 分页查询
      if (pageNum) {
        this.pageNum = pageNum;
      }
      request.get('/writeOff/selectByPage', {
        params: { pageNum: this.pageNum, pageSize: this.pageSize, phone: this.phone}
      }).then(res => {
        if (res && res.data && typeof res.data === 'object') {
          this.tableData = res.data.list || [];
          this.total = res.data.total || 0;
          console.log("订单数据:", this.tableData);
        } else {
          console.error("无效的响应数据:", res);
          ElMessage.error('获取订单数据失败，请稍后再试');
        }
      }).catch(err => {
        console.error("获取订单数据失败:", err);
      });
    },

    handleCurrentChange(pageNum) {
      this.load(pageNum);
    },

    handleSelectionChange(rows) {
      this.ids = rows.map(item => item.id);
    },
    writeOff(row) {

      request.put(`/writeOff/update`,
          {orderNum: row.orderNum}
      ).then(response => {
          if (response.code === 1) {
            ElMessage.success('核销成功');
            this.load();
          } else {
            ElMessage.error(response.msg);
          }
      });
    }

  }
}
</script>

<style scoped>

</style>